<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <!--        <el-form-item>-->
        <!--          <el-button type="primary" @click="handleAdd">新增</el-button>-->
        <!--        </el-form-item>-->
      </el-form>
    </el-col>

    <!--列表:优秀实践，copy domain修改列表-->
    <el-table :data="pageData.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="title" label="寻主标题" width="100" sortable>
      </el-table-column>
      <el-table-column prop="name" label="宠物名称" width="100" sortable>
      </el-table-column>
      <el-table-column prop="price" label="售价" width="120" sortable>
      </el-table-column>

      <el-table-column prop="age" label="宠物年龄" width="120" sortable>
      </el-table-column>
      <el-table-column prop="gender" label="宠物性别" width="120">
        <template slot-scope="{ row }">
          <span v-if="row.gender">公</span>
          <span v-else>母</span>
        </template>
      </el-table-column>
      <el-table-column prop="coatColor" label="宠物毛色" width="120" sortable>
      </el-table-column>
      <el-table-column prop="address" label="地址" width="200" sortable>
      </el-table-column>

      <el-table-column label="操作" min-width="120">
        <template scope="scope">
          <el-button style="color: #11b95c" size="small" @click="handleEdit(scope.$index, scope.row)">通过</el-button>
          <el-button style="color: #ff4d51" size="small" @click="returnMsg(scope.$index, scope.row)">回执</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="query.currentPage"
                     :page-size="query.pageSize" :total="pageData.totals" style="float:right;">
      </el-pagination>
    </el-col>

    <!--回执
        v-model="editFormVisible" 老版本打开对话框
    -->
    <el-dialog title="填写回执信息" :visible.sync="boxForTheReturnReceipt" :close-on-click-modal="false">


      <!--ref="searchMasterMsgForm" 指定表单名称，以后可以通过this.$refs.searchMasterMsgForm-->
      <el-form :model="acceptOrderForm" label-width="80px" :rules="acceptSearchMasterMsgFormRules"
               ref="acceptSearchMasterMsgForm">
        <!--        <el-form-item label="处理人">-->
        <!--下拉框-->
        <!--          <el-select v-model="acceptOrderForm.handler" value-key="id" placeholder="请选择" style="width: 350px">-->
        <!--            <el-option-->
        <!--                v-for="employee in employees"-->
        <!--                :key="employee.id"-->
        <!--                :label="employee.username"-->
        <!--                :value="employee">-->
        <!--              <span style="float: left">{{ employee.username }}</span>-->
        <!--              <span style="float: right; color: #8492a6; font-size: 13px">{{ employee.email }}</span>-->
        <!--            </el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="备注" prop="sn">
          <el-input v-model="acceptOrderForm.note" auto-complete="off"></el-input>
        </el-form-item>
        回执后，该用户发布会信息不会展示在平台，请谨慎操作！
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="boxForTheReturnReceipt = false">取消</el-button>
        <el-button type="primary" @click.native="returnmethod" :loading="acceptSearchMasterMsgLoading">确定</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
export default {
  data() {
    return {
      title: '',
      pageData: {
        totals: 0,//分页条
        rows: []//数据表格
      },
      query: {
        keyword: '',
        currentPage: 1,//分页条
        pageSize: 10 //分页条
      },
      listLoading: false,
      sels: [],//列表选中列
      searchMasterMsgFormVisible: false,//编辑界面是否显示
      boxForTheReturnReceipt: false,// 回执的界面是否展示
      acceptSearchMasterMsgLoading: false,
      searchMasterMsgLoading: false,
      acceptSearchMasterMsgFormRules: {
        note: [
          {required: true, message: '请输入备注！', trigger: 'blur'}
        ]
      },

      //编辑界面数据
      searchMasterMsg: {
        id: null,
        name: '',
        sn: '',
        dirPath: '',
        state: 0,
        manager: {},
        parent: null
      },

      //回执界面数据
      acceptOrderForm: {
        id: null,
        note: ''
      },
    }
  },
  methods: {
    search() {
      //修改页码
      this.query.currentPage = 1;
      //改查询条件=双向绑定输入时自动改变，只需要重新查询一次就ok
      this.getSearchMasterMsgs();
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getSearchMasterMsgs();
    },
    //获取宠物寻主列表
    getSearchMasterMsgs() {
      this.listLoading = true; //加载条
      this.$http.post("/searchMasterMsg/toAudit", this.query)
          .then(result => {
            this.pageData = result.data;
            this.listLoading = false; //去除加载条
          })
          .catch(result => {
            alert("抱错了！");
          })

    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let url = "/searchMasterMsg/" + row.id;
        this.$http.delete(url)
            .then((result) => {
              console.log(result)
              result = result.data; //{sucesss:true,message:"xx"}
              this.listLoading = false;
              if (result.success) { //成功提示
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              } else {
                this.$message({ //错误提示
                  message: result.msg,
                  type: 'error'
                });
              }
              //重新加载数据
              this.getSearchMasterMsgs();
            });
      }).catch(() => {
      });
    },

    //显示审核界面
    returnMsg: function (index, row) {
      console.log(row);
      this.boxForTheReturnReceipt = true;

      this.acceptOrderForm.id = row.id;
      // if (row.shop) {
      //   let shopId = row.shop.id;
      //   this.getEmployees(shopId);
      // }

    },


    //回执
    returnmethod: function (index, row) {
      this.$confirm('确认回执吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let url = "/searchMasterMsg/returnMsg"
        this.$http.post(url, {
          // 消息id
          msgId: this.acceptOrderForm.id,
          // 接单备注
          note: this.acceptOrderForm.note
        })
            .then((result) => {
              result = result.data; //{sucesss:true,message:"xx"}
              this.listLoading = false;
              if (result.success) { //成功提示
                this.$message({
                  message: '回执成功',
                  type: 'success'
                });
              } else {
                this.$message({ //错误提示
                  message: result.msg,
                  type: 'error'
                });
              }
              this.boxForTheReturnReceipt = false; // 关闭回执框
              //重新加载数据
              this.getSearchMasterMsgs();
            });
      }).catch(() => {
      });
    },


    //显示编辑界面
    handleEdit: function (index, row) {
      // //修改对话框标题
      // this.title = "修改部门"
      // this.searchMasterMsgFormVisible = true;
      // //回显
      // this.searchMasterMsg = Object.assign({}, row);

      this.$confirm('确认上架此寻主消息吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let url = "/searchMasterMsg/risePetMsg/" + row.id;
        this.$http.get(url)
            .then((result) => {
              console.log(row.id)
              result = result.data; //{sucesss:true,message:"xx"}
              this.listLoading = false;
              if (result.success) { //成功提示
                this.$message({
                  message: '上架成功',
                  type: 'success'
                });
              } else {
                this.$message({ //错误提示
                  message: result.msg,
                  type: 'error'
                });
              }
              //重新加载数据
              this.getSearchMasterMsgs();
            });
      }).catch(() => {
      });


    },
    //显示新增界面
    handleAdd: function () {
      //修改对话框标题
      this.title = "添加部门"
      //弹出对话框
      this.searchMasterMsgFormVisible = true;
      //置空数据，防止先点修改，后点添加有值
      this.searchMasterMsg = {
        id: null,
        name: '',
        sn: '',
        dirPath: '',
        state: 0,
        manager: {},
        parent: null
      };
    },
    //保存
    save: function () {
      this.$refs.searchMasterMsgForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认保存吗？', '提示', {}).then(() => {
            this.searchMasterMsgLoading = true;
            let para = Object.assign({}, this.searchMasterMsg);
            console.log(this.searchMasterMsg.parent)
            //处理上级部门参数格式 parent:[1,34]---->parent:{id:34}
            let parentArr = para.parent;
            if (parentArr) {
              para.parent = {"id": parentArr[parentArr.length - 1]}
            }
            this.$http.put("/searchMasterMsg", para)
                .then((result) => {
                  this.searchMasterMsgLoading = false;
                  result = result.data;
                  if (result.success) {
                    this.$message({
                      message: '提交成功',
                      type: 'success'
                    });
                    this.searchMasterMsgFormVisible = false; //关闭对话框
                    this.$refs['searchMasterMsgForm'].resetFields();//重置表单
                    this.getSearchMasterMsgs(); //重新加载
                  } else {
                    this.$message({
                      message: result.msg,
                      type: 'error'
                    });
                    //失败了可能修改了提交
                  }
                });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      //遍历选择记录把id放到一个数组中
      var ids = this.sels.map(item => item.id); //[1,2,3]
      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/searchMasterMsg", ids)
            .then((result) => {
              this.listLoading = false;
              result = result.data;
              if (result.success) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              } else {
                this.$message({
                  message: result.msg,
                  type: 'error'
                });
              }
              //跳转到第一页
              this.handleCurrentChange(1);
            });
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getSearchMasterMsgs();
  }
}

</script>

<style scoped>

</style>